﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 选择合格原料 : System.Web.UI.Page
    {
        private string ss=
@"select
管材到货登记.ID,
原料.品名,
原料.规格,
原料供货单位.名称 as 供货单位,
管材到货登记.车号,
管材到货登记.磅量,
管材到货登记.到货日期
from 管材到货登记
inner join 原料 on 管材到货登记.原料ID=原料.ID
inner join 原料供货单位 on 管材到货登记.原料供货单位ID=原料供货单位.ID";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string opp3 = (string)Session["OPP3"];
#if DEBUG
                opp3 = ",1,2,3,4,5,6,";
#endif
                if (opp3 == null || opp3 == ",")
                {
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = false;
                    this.Panel_Msg.Visible = true;
                }
                else
                {
                    if (opp3.StartsWith(","))
                    {
                        opp3 = opp3.Substring(1);
                    }
                    if (opp3.EndsWith(","))
                    {
                        opp3 = opp3.Substring(0, opp3.Length - 1);
                    }
                    this.SqlDataSource2.SelectCommand = "select [ID],[品名] from [原料] where [ID] in (" + opp3 + ")";
                }
            }
            else
            {
                string id = this.SelectedList.Text;
                if (id.StartsWith(","))
                {
                    id = id.Substring(1);
                }
                if (id.EndsWith(","))
                {
                    id = id.Substring(0, id.Length - 1);
                }
                if (id != "")
                {
                    this.SqlDataSource3.SelectCommand = ss + (string)this.ViewState["SelectCommand"] + " and 管材到货登记.[ID] not in(" + id + ")";
                }
                else
                {
                    this.SqlDataSource3.SelectCommand = ss+(string)this.ViewState["SelectCommand"];
                }

            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 条";
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }

        protected void GridView2_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager2.Text = (this.GridView2.PageIndex + 1).ToString();
            this.Label_Pages2.Text = " 共" + this.GridView2.PageCount.ToString() + "页 ";

            this.Button_Pager_First2.CssClass = "pageturn-first-b";
            this.Button_Pager_First2.Enabled = true;
            this.Button_Pager_Prev2.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev2.Enabled = true;
            this.Button_Pager_Next2.CssClass = "pageturn-next-b";
            this.Button_Pager_Next2.Enabled = true;
            this.Button_Pager_Last2.CssClass = "pageturn-last-b";
            this.Button_Pager_Last2.Enabled = true;
            if (this.GridView2.PageIndex == 0)
            {
                this.Button_Pager_Prev2.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev2.Enabled = false;
            }
            if (this.GridView2.PageIndex < 2)
            {
                this.Button_Pager_First2.CssClass = "pageturn-first-g";
                this.Button_Pager_First2.Enabled = false;
            }
            if (this.GridView2.PageIndex == this.GridView2.PageCount - 1 || this.GridView2.PageCount == 0)
            {
                this.Button_Pager_Next2.CssClass = "pageturn-next-g";
                this.Button_Pager_Next2.Enabled = false;

            }
            if (this.GridView2.PageIndex > this.GridView2.PageCount - 3)
            {
                this.Button_Pager_Last2.CssClass = "pageturn-last-g";
                this.Button_Pager_Last2.Enabled = false;
            }
        }
        protected void GridView2_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView2_DataBound(null, null);
        }
        protected void SqlDataSource4_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total2.Text = "已选择 " + e.AffectedRows + " 条";
            this.Button_NextStep.Visible = true;
        }
        protected void Pager2_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView2.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView2.PageIndex > 0) this.GridView2.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView2.PageIndex < this.GridView2.PageCount - 1) this.GridView2.PageIndex++;
                        break;
                    case "++":
                        this.GridView2.PageIndex = this.GridView2.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView2.PageCount)
                    {
                        this.GridView2.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView2.PageIndex + 1).ToString();
            }
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string sql = " where 管材到货登记.入库状态=0 and 质检结果=1 and 管材到货登记.原料供货单位ID=" + this.GongHuo.Text;
            sql += " and 管材到货登记.原料ID=" + this.YuanLiao.Text;
            DateTime d1, d2;
            if (this.Date1.Text != "")
            {
                if (DateTime.TryParse(this.Date1.Text, out d1))
                {
                    sql += " and 管材到货登记.到货日期>='" + d1.ToShortDateString() + "'";
                }
            }

            if (this.Date2.Text != "")
            {
                if (DateTime.TryParse(this.Date2.Text, out d2))
                {
                    sql += " and 管材到货登记.到货日期<='" + d2.ToShortDateString() + "'";
                }
            }
            this.SqlDataSource3.SelectCommand = ss + sql;
            this.ViewState["SelectCommand"] = sql;
            this.SelectedList.Text = ",";
            this.SqlDataSource4.SelectCommand = "";
            Label_Total2.Text = "已选择 0 条";
            this.Button_NextStep.Visible = false;
            this.GridView2.DataBind();

        }
        private void ReQuery()
        {
            string id = this.SelectedList.Text;
            if (id.StartsWith(","))
            {
                id = id.Substring(1);
            }
            if (id.EndsWith(","))
            {
                id = id.Substring(0, id.Length - 1);
            }
            if (id != "")
            {
                string sql = "select [ID],车号,磅量,到货日期 from 管材到货登记 where [id] in(" + id + ")";
                this.SqlDataSource4.SelectCommand = sql;
                this.SqlDataSource3.SelectCommand = ss + (string)this.ViewState["SelectCommand"] + " and 管材到货登记.[ID] not in(" + id + ")";
            }
            else
            {
                this.SqlDataSource4.SelectCommand = "";
                this.SqlDataSource3.SelectCommand = ss + (string)this.ViewState["SelectCommand"];
            }
            Label_Total2.Text = "已选择 0 条";
            this.Button_NextStep.Visible = false;
            this.GridView2.DataBind();
        }

        protected void Button_Pick_Click(object sender, EventArgs e)
        {
            string id=((Button)sender).CommandName;
            this.SelectedList.Text+=id+",";
            ReQuery();
        }

        protected void Button_UnPick_Click(object sender, EventArgs e)
        {
            string id = ((Button)sender).CommandName;
            this.SelectedList.Text = this.SelectedList.Text.Replace(","+id + ",",",");
            ReQuery();
        }

        protected void Button_NextStep_Click(object sender, EventArgs e)
        {
            string id = this.SelectedList.Text;
            if (id.StartsWith(","))
            {
                id = id.Substring(1);
            }
            if (id.EndsWith(","))
            {
                id = id.Substring(0, id.Length - 1);
            }
            Response.Redirect("原料入库单.aspx?dhlist=" + id);
        }
    }
}
